﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Media;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Drawing.Drawing2D;

namespace GameSquares
{
    public static class DrawingOnForm 
    {
        //Розбиття комірок
        public static void ComputeCellsCoordinate(Form form, int BackdownHeight, GraphItem[] gItems)//Малюєм поле
         {
            int num = (Global.NumGraphItems / 10);
            int lenside = 0;

            int width = form.ClientRectangle.Width;
            int height = form.ClientRectangle.Height - BackdownHeight;

            lenside = height - 20;

            int deltaY = height - lenside;
            int lenCell = lenside / num;

            int count = 0;
            for (int x = 0; x < num; x++)
            {
                for (int y = 0; y < num; y++)
                {
                    gItems[count].CellCoordinate = new Rectangle(x * lenCell + 10, y * lenCell + deltaY / 2 + BackdownHeight, lenCell, lenCell);
                    count++;
                }
            }
        }
        
        //Надання графічним елементам випадкових кольорів
       public  static void InitNewGame(GameProcess NG)
        {
            Random rand = new Random(Environment.TickCount);
            // Будуємо поле Випадкових кольорів
            for (int i = 0; i < Global.NumGraphItems; i++)
            {
                int r = rand.Next(Global.AmountColorBalls);
                NG.GItems[i].Color = NG.GameSet.GraphItems[r];
                NG.GItems[i].Visible = true;
            }
            for (int i = 0; i < NG.GameSet.DRH.Length; i++)
            {
                if (NG.GameSet.DRH[i] != null)
                    NG.GameSet.DRH[i].CurrentPlayer = false;
            }


        }

        //Малювання панелі рекордів та очків гравця
       public static void PaintSidebar(Graphics g, int BackdownHeight, GameProcess GP)
        {
            //Бічна панель
            Font font = new Font("Times New Roman", 20);
            int x = GP.GItems[90].CellCoordinate.X + GP.GItems[90].CellCoordinate.Width + 5;
            int y = 40 + BackdownHeight;

            for (int i = 0; i < GP.GameSet.DRH.Length; i++)
            {
                if (GP.GameSet.DRH[i].CurrentPlayer == true)
                {
                    g.DrawString(GP.GameSet.DRH[5].Name, font, new SolidBrush(Color.Yellow), x, y);

                    y = 80 + BackdownHeight;
                    g.DrawString("Очки", font, new SolidBrush(Color.White), x, y);
                    g.DrawString("\t " + GP.GameSet.DRH[5].Score.ToString(), font, new SolidBrush(Color.Yellow), x, y);
                    break;
                }
            }

            //Рекорди 
            g.DrawString("Рекорди:", font, new SolidBrush(Color.LightYellow), x, y + 80);

            y += 120;
            for (int i = 0; i < GP.GameSet.DRH.Length - 0; i++)
            {
                if (GP.GameSet.DRH[i] != null && GP.GameSet.DRH[i].Score > 0)
                {
                    if (GP.GameSet.DRH[i].CurrentPlayer == false)
                    {
                        g.DrawString(GP.GameSet.DRH[i].Name, font, new SolidBrush(Color.White), x, y + i * 20);
                        g.DrawString("\t " + GP.GameSet.DRH[i].Score, font, new SolidBrush(Color.LightGreen), x, y + i * 20);
                    }
                }
            }

        }

        //КІсточка для Графічної фігури
       public static Brush BrushCell(GraphItem GI)
       {
           // Підготовка координат для  ромба
           Point point1 = new Point(GI.CellCoordinate.Left + GI.CellCoordinate.Width / 2, GI.CellCoordinate.Top);
           Point point2 = new Point(GI.CellCoordinate.Left + GI.CellCoordinate.Width, GI.CellCoordinate.Top + GI.CellCoordinate.Height / 2);
           Point point3 = new Point(GI.CellCoordinate.Left + GI.CellCoordinate.Width / 2, GI.CellCoordinate.Top + GI.CellCoordinate.Height);
           Point point4 = new Point(GI.CellCoordinate.Left, GI.CellCoordinate.Top + GI.CellCoordinate.Height / 2);

           Point[] pt = { point1, point2, point3, point4 };

           // Яка фігура така і кість
           GraphicsPath gp = new GraphicsPath();

           gp.AddRectangle(GI.CellCoordinate);



           //Розмальовування квадрата
           PathGradientBrush pathBrush = new PathGradientBrush(gp);
           pathBrush.SurroundColors = new Color[] { Color.FromArgb(50, 50, 50) };
           pathBrush.CenterPoint = new PointF(GI.CellCoordinate.Left + GI.CellCoordinate.Width / 2, GI.CellCoordinate.Top + GI.CellCoordinate.Height / 2);
           pathBrush.CenterColor = Color.DarkGray;

           return pathBrush;
       }

        //Малюємо графічну фігуру
        public static void Draw(GraphItem GI,Graphics g)
        {
            g.FillRectangle(BrushCell(GI), GI.CellCoordinate.X + 1,
                GI.CellCoordinate.Y + 1, GI.CellCoordinate.Width - 1, GI.CellCoordinate.Height - 1);


            if (GI.Visible == false) return;


            Rectangle rectInflate = GI.CellCoordinate;
            rectInflate.Inflate(GI.DeltaWidth, GI.DeltaHeight);

            //Формування квадрата в комірці

            // Підготовка координат для ромба
            Point point1 = new Point(GI.CellCoordinate.Left + GI.CellCoordinate.Width / 2, rectInflate.Top);
            Point point2 = new Point(rectInflate.Left + rectInflate.Width, GI.CellCoordinate.Top + GI.CellCoordinate.Height / 2);
            Point point3 = new Point(GI.CellCoordinate.Left + GI.CellCoordinate.Width / 2, rectInflate.Top + rectInflate.Height);
            Point point4 = new Point(rectInflate.Left, GI.CellCoordinate.Top + GI.CellCoordinate.Height / 2);

            Point[] pt = { point1, point2, point3, point4 };

            // Выбір виду фігури.
            GraphicsPath gp = new GraphicsPath();
            switch (GraphItem.CurrentTypeGraphItem)
            {
                case GameSquares.GraphItem.TypeGraphItem.tEllipse:
                    gp.AddEllipse(rectInflate);
                    break;
                case GameSquares.GraphItem.TypeGraphItem.tRectangle:
                    gp.AddRectangle(rectInflate);
                    break;
                case GameSquares.GraphItem.TypeGraphItem.tRhombus:
                    gp.AddPolygon(pt);
                    break;
            }
          
            if (rectInflate.Width < 0 || rectInflate.Height < 0) return;

            // Малювання поточного квадрата
            PathGradientBrush pathBrush = new PathGradientBrush(gp);
            pathBrush.SurroundColors = new Color[] { GI.Color };
            pathBrush.CenterPoint = new PointF(GI.CellCoordinate.Left + GI.CellCoordinate.Width / 2, GI.CellCoordinate.Top + GI.CellCoordinate.Height / 2);
            pathBrush.CenterColor = Color.White;

            switch (GraphItem.CurrentTypeGraphItem)
            {
                case GameSquares.GraphItem.TypeGraphItem.tEllipse:
                    g.FillEllipse(pathBrush, rectInflate);
                    break;
                case GameSquares.GraphItem.TypeGraphItem.tRectangle:
                    g.FillRectangle(pathBrush, rectInflate);
                    break;
                case GameSquares.GraphItem.TypeGraphItem.tRhombus:
                    g.FillPolygon(pathBrush, pt);
                    break;
            }

        }
    }
}
